package com.lu;

import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncodingAttributes;
import it.sauronsoftware.jave.IgnoreErrorEncoder;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

/**
 * @program RCS
 * @description: ffmpeg工具类
 *  官方文档：https://ffmpeg.org/ffmpeg.html
 *      sudo ffmpeg -i /Users/zhanglu/Desktop/lu/learning/视频制作与运营/lu-demo/玛卡巴卡/1.mp3  -f wav /Users/zhanglu/Desktop/lu/learning/视频制作与运营/lu-demo/玛卡巴卡/1.wav
     *      ffmpeg -i /Users/zhanglu/Desktop/lu/learning/视频制作与运营/lu-demo/玛卡巴卡/1.wav -ar 8000 -ab 12.2k -ac 1 /Users/zhanglu/Desktop/lu/learning/视频制作与运营/lu-demo/玛卡巴卡/1.amr
 *      https://blog.csdn.net/bang152101/article/details/106147264/
 * @author: zhanglu
 * @create: 2020-11-17 10:35:00
 */
@Slf4j
public class FfmpegUtils {

    /**
     *  MaaP返回amr音频文件，RCS转为8K采样率16位PCM线性wav：
     *  ffmpeg -i 02.amr -acodec pcm_s16le -ar 8000 02.wav
     * @param source
     * @param target
     * @return
     */
    public static boolean voicePlatformConvertWav(File source, File target){
        boolean res = true;
        try {
            AudioAttributes audio = new AudioAttributes();
            audio.setCodec("pcm_s16le");
            audio.setSamplingRate(8000);
            EncodingAttributes attrs = new EncodingAttributes();
            attrs.setFormat("amr");
            attrs.setAudioAttributes(audio);
            Encoder encoder = new IgnoreErrorEncoder();
            encoder.encode(source, target, attrs);
        } catch (Exception e) {
            log.error("voicePlatformConvertWav-Exception:" + e);
            res = false;
        }
        return res;
    }

    public static void main(String[] args) {
        voicePlatformConvertWav(new File("/Users/zhanglu/Desktop/lu/learning/视频制作与运营/lu-demo/玛卡巴卡/1.mp3"),
                new File("/Users/zhanglu/Desktop/lu/learning/视频制作与运营/lu-demo/玛卡巴卡/1.amr"));
    }

}
